package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Role;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.RoleMapper;
import com.example.server.web.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Page<Role> getRolesPage(PageQueryDto pageQueryDto) {
        String roleName = "";
        Integer enabled = null;
        Integer page = pageQueryDto.getPage();
        Integer limit = pageQueryDto.getLimit();
        enabled = (Integer) pageQueryDto.getParams("enabled");
        roleName = (String) pageQueryDto.getParams("roleName");
        Page<Role> p = new Page<>(page,limit);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(roleName),Role::getRoleName,roleName)
                .eq(enabled != null,Role::getEnabled,enabled);
        roleMapper.selectPage(p,queryWrapper);

        return p;
    }



    @Override
    public Role getRoleById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public Integer insertRole(Role role) {
        String roleName = role.getRoleName();
        if (roleName == null){
            throw new CustomException("角色名字不能为空");
        }
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName,roleName);
        Role role2 = this.getOne(wrapper);
        //判断角色名字是否已存在
        if(role2!=null){
            throw new CustomException("角色名字已存在");
        }
        return roleMapper.insert(role);
    }

    @Override
    public Integer updateRole(Role role) {
        String roleName = role.getRoleName();
        if (!StringUtils.isNotBlank(roleName)){
            throw new CustomException("角色名字不能为空");
        }
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName,roleName);
        Role role2 = this.getOne(wrapper);
        //判断角色名字是否已存在
        if(role2!=null&&role2.getId()!=role.getId()){
            throw new CustomException("角色名字已存在");
        }
        return roleMapper.updateById(role);
    }

    @Override
    public Integer deleteRole(Long roleId) {
        if(roleId==null){
            throw new CustomException("角色id不能为空");
        }
        return roleMapper.deleteById(roleId);
    }

    @Override
    public Integer deleteRoles(List<Long> roleIdList) {
        if (roleIdList!=null){
            throw new CustomException("角色id不能为空");
        }
        return roleMapper.deleteBatchIds(roleIdList);
    }

}
